﻿using FictionsDownload.BLL;
using FictionsDownload.Events;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static FictionsDownload.Static.FormEx;

namespace FictionsDownload.UI
{
    public partial class FormReadView : Form
    {
        #region 公共属性和事件
        /// <summary>
        /// 选中章节
        /// </summary>
        public event EventHandler<SelectionDirectoryEventArgs> SelectionDirectory;
        /// <summary>
        /// 下一章
        /// </summary>
        public event EventHandler NextDirectory;
        /// <summary>
        /// 上一章
        /// </summary>
        public event EventHandler PrevDirectory;
        #endregion
        #region 单例初始化
        private FormReadView()
        {
            InitializeComponent();
            //
            this.TopLevel = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Show();
            //
        }
        private static FormReadView Instance;
        private static readonly object lockRoot = new object();
        /// <summary>
        /// 初始化实例
        /// </summary>
        public static FormReadView GetIntance
        {
            get
            {
                if (Instance == null)
                {
                    lock (lockRoot)
                    {
                        if (Instance == null)
                        {
                            Instance = new FormReadView();
                        }
                    }
                }
                return Instance;
            }
        }
        #endregion
        #region 似有属性和变量
        private FormReadDirectory FormReadDirectory { get; set; }
        private FormReadText FormReadText { get; set; }
        #endregion
        #region 控件初始化
        /// <summary>
        /// 窗口初始化之后，控件再做调整
        /// </summary>
        public void InitializeComponentCode()
        {
            pl_ReadDirectoryInit();
            pl_ReadTextInit();
        }
        private void pl_ReadDirectoryInit()
        {
            //容器位置调整
            this.pl_ReadDirectory.Location = new Point(0,0);
            this.pl_ReadDirectory.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
            this.pl_ReadDirectory.Visible = true;
            //实例化
            this.FormReadDirectory = FormReadDirectory.GetIntance;
            this.FormReadDirectory.Parent = this.pl_ReadDirectory;
            this.FormReadDirectory.Dock = DockStyle.Fill;
            FormReadDirectory.InitializeComponentCode();
            //事件
            this.FormReadDirectory.SelectionDirectory += new EventHandler<SelectionDirectoryEventArgs>(FormReadDirectory_SelectionDirectory);
        }
        private void FormReadDirectory_SelectionDirectory(object sender,SelectionDirectoryEventArgs e)
        {
            this.SelectionDirectory?.Invoke(this, e);
            this.SetReadText(e.DirectoryID);
        }
        private void pl_ReadTextInit()
        {
            this.pl_ReadText.Location = new Point(0, 0);
            this.pl_ReadText.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
            this.pl_ReadText.Visible = true;
            this.pl_ReadText.BringToFront();
            //
            this.FormReadText = FormReadText.GetIntance;
            this.FormReadText.Parent = this.pl_ReadText;
            this.FormReadText.Dock = DockStyle.Fill;
            this.FormReadText.InitializeComponentCode();
            this.FormReadText.PrevDirectory += new EventHandler(FormReadTextBox_PrevDirectory);
            this.FormReadText.NextDirectory += new EventHandler(FormReadTextBox_NextDirectory);
        }
        #endregion
        private void FormReadTextBox_PrevDirectory(object sender, EventArgs e)
        {
            this.PrevDirectory?.Invoke(this, null);
        }
        private void FormReadTextBox_NextDirectory(object sender, EventArgs e)
        {
            this.NextDirectory?.Invoke(this, null);
        }
        /// <summary>
        /// 传入书籍ID显示章节列表
        /// </summary>
        /// <param name="bookId"></param>
        public void SetBook(string bookId)
        {
            FormReadDirectory.SetBook(bookId);
            pl_ReadText.Visible = false;
        }
        /// <summary>
        /// 传入章节ID显示文本
        /// </summary>
        /// <param name="directoryId"></param>
        public void SetReadText(string directoryId)
        {
            pl_ReadText.Visible = true;
            FormReadText.SetReadText(directoryId);
        }
    }
}
